package com.sino.second.business.service;

import java.util.ArrayList;
import java.util.List;

import com.sino.bean.SysZhuanyema;
import com.sino.bean.TblZhuanjiarukuxinxibiao;
import com.sino.dao.IExpertManageDAO;
import com.sino.dao.IStaticDataDAO;
import com.sino.model.StaticModel;
import com.sino.second.bean.SysXmpsmc;
import com.sino.second.bean.TblZjpsfp;
import com.sino.second.business.IZjpsfpService;
import com.sino.second.dao.IZjpsfpDAO;
import com.sino.second.dao.hibernate.BaseDataDAO;
import com.sino.second.model.BaseModel;
import com.sino.second.model.ZjpsfpModel;
import com.sino.util.SysParameter;

public class ZjpsfpServiceImp implements IZjpsfpService {
	private IZjpsfpDAO zjpsfpDAO;// 引入zjpsfpDAO

	private BaseDataDAO baseDataDAO;// 引入baseDataDAO

	private IExpertManageDAO expertManageDAO;// 引入expertManageDAO

	private IStaticDataDAO staticDataDAO;// 引入staticDataDAO

	/**
	 * @return the zjpsfpDAO
	 */
	public IZjpsfpDAO getZjpsfpDAO() {
		return zjpsfpDAO;
	}

	/**
	 * @param zjpsfpDAO
	 *            the zjpsfpDAO to set
	 */
	public void setZjpsfpDAO(IZjpsfpDAO zjpsfpDAO) {
		this.zjpsfpDAO = zjpsfpDAO;
	}

	/**
	 * @return the baseDataDAO
	 */
	public BaseDataDAO getBaseDataDAO() {
		return baseDataDAO;
	}

	/**
	 * @param baseDataDAO
	 *            the baseDataDAO to set
	 */
	public void setBaseDataDAO(BaseDataDAO baseDataDAO) {
		this.baseDataDAO = baseDataDAO;
	}

	/**
	 * @return the expertManageDAO
	 */
	public IExpertManageDAO getExpertManageDAO() {
		return expertManageDAO;
	}

	/**
	 * @param expertManageDAO
	 *            the expertManageDAO to set
	 */
	public void setExpertManageDAO(IExpertManageDAO expertManageDAO) {
		this.expertManageDAO = expertManageDAO;
	}

	/**
	 * @return the staticDataDAO
	 */
	public IStaticDataDAO getStaticDataDAO() {
		return staticDataDAO;
	}

	/**
	 * @param staticDataDAO
	 *            the staticDataDAO to set
	 */
	public void setStaticDataDAO(IStaticDataDAO staticDataDAO) {
		this.staticDataDAO = staticDataDAO;
	}

	/**
	 * 列出所有专业，并将列表转化成List<StaticModel>
	 */
	public List<StaticModel> listAllZhuanye() {
		List<StaticModel> smList = new ArrayList<StaticModel>();
		List<SysZhuanyema> zhuanyemaList = this.staticDataDAO.findZhuanyema();
		for (SysZhuanyema szy : zhuanyemaList) {
			StaticModel sm = this.getStaticModelByZhuanyeMa(szy);
			smList.add(sm);
		}
		return smList;
	}

	/**
	 * 按照评审组列出符合条件的专家信息
	 */
	public List<BaseModel> listReviewExpertByXmpsmc(String xmpsmcId,
			String hqlWhere) {
		List<TblZjpsfp> zjpsfpList = this.zjpsfpDAO.listZjpsfpByXmpsmcId(
				xmpsmcId, hqlWhere);// 通过xmpsmcId查出，被分配给xmpsmcId组的所有专家信息
		List<Integer> spExpertIdList = new ArrayList<Integer>();// 新建一个ArrayList
		for (TblZjpsfp tzps : zjpsfpList) {
			spExpertIdList.add(tzps.getTblZhuanjiarukuxinxibiao().getFBh());// 将已经分配给xmpsmcId的专家id存入spExpertIdList，用于下面的比对
		}

		List<TblZhuanjiarukuxinxibiao> expertList = this.expertManageDAO
				.findAllExperts();// 查出所有专家信息
		List<BaseModel> baseModelList = new ArrayList<BaseModel>();// 新建一个ArrayList
		for (TblZhuanjiarukuxinxibiao tzj : expertList) {
			BaseModel bm = this.getBaseModelByExpert(tzj);
			if (this.isExitInArray(tzj.getFBh(), spExpertIdList)) {// 如果该专家已经被分配，则将checkFlag标志置为true
				bm.setCheckFlag(true);
			}
			baseModelList.add(bm);
		}
		return baseModelList;
	}

	public List<BaseModel> listThirdReviewExpert(String hqlWhere) {
		List<TblZjpsfp> zjpsfpList = this.zjpsfpDAO.listZjpsfpByXmpsmcId(
				SysParameter.THIRDREVIEW, hqlWhere);// 通过xmpsmcId查出，被分配给xmpsmcId组的所有专家信息
		List<Integer> spExpertIdList = new ArrayList<Integer>();// 新建一个ArrayList
		for (TblZjpsfp tzps : zjpsfpList) {
			spExpertIdList.add(tzps.getTblZhuanjiarukuxinxibiao().getFBh());// 将已经分配给xmpsmcId的专家id存入spExpertIdList，用于下面的比对
		}
       
		List<TblZhuanjiarukuxinxibiao> expertList = this.expertManageDAO
				.findThirdReviewExperts();// 查出所有大评委专家信息
		List<BaseModel> baseModelList = new ArrayList<BaseModel>();// 新建一个ArrayList
		for (TblZhuanjiarukuxinxibiao tzj : expertList) {
			BaseModel bm = this.getBaseModelByExpert(tzj);
			if (this.isExitInArray(tzj.getFBh(), spExpertIdList)) {// 如果该专家已经被分配，则将checkFlag标志置为true
				bm.setCheckFlag(true);
			}
			baseModelList.add(bm);
		}
		return baseModelList;
	}

	/**
	 * 按照评审组和专业列出符合条件的专家信息
	 */
	public List<BaseModel> listReviewExpertByXmpsmcAndZhuanye(String xmpsmcId,
			String zhuanyeId, String hqlWhere) {
		List<TblZjpsfp> zjpsfpList = this.zjpsfpDAO.listZjpsfpByXmpsmcId(
				xmpsmcId, hqlWhere);
		List<Integer> spExpertIdList = new ArrayList<Integer>();
		for (TblZjpsfp tzps : zjpsfpList) {
			spExpertIdList.add(tzps.getTblZhuanjiarukuxinxibiao().getFBh());
		}

		List<TblZhuanjiarukuxinxibiao> expertList = this.expertManageDAO
				.findExpertByZhuanyema(zhuanyeId);
		List<BaseModel> baseModelList = new ArrayList<BaseModel>();
		for (TblZhuanjiarukuxinxibiao tzj : expertList) {
			BaseModel bm = this.getBaseModelByExpert(tzj);
			if (this.isExitInArray(tzj.getFBh(), spExpertIdList)) {
				bm.setCheckFlag(true);
			}
			baseModelList.add(bm);
		}
		return baseModelList;
	}

	public Integer findZjpsfpCountByXmpsmcId(String xmpsmcId, String zhuanyeId,
			String hqlWhere) {
		return this.getZjpsfpDAO().findZjpsfpCountByXmpsmcId(xmpsmcId,
				zhuanyeId, hqlWhere);
	}

	/**
	 * 增加或者删除一个专家分配信息
	 */
	public void saveOrDeleteZjpsfp(String xmpsmcId, String zhuanyeId,
			String[] expertIdArray, ZjpsfpModel zjpsfpModel, String hqlWhere) {
		List<TblZjpsfp> zjpsfpList = new ArrayList<TblZjpsfp>(); // 新建一个arrayList
		if (null == zhuanyeId || "".equals(zhuanyeId)) {// 判断如果zhuanyeId不存在或者为空
			zjpsfpList = this.zjpsfpDAO
					.listZjpsfpByXmpsmcId(xmpsmcId, hqlWhere);// 调用zjpsfpDAO的方法查出所有符合审核组的分配信息
		} else { // 否则
			zjpsfpList = this.zjpsfpDAO.listZjpsfpByXmpsmcId(xmpsmcId,// 调用zjpsfpDAO的方法查出所有符合审核组和专业的分配信息
					zhuanyeId, hqlWhere); //
		}

		if (null != zjpsfpList && zjpsfpList.size() > 0) {// 如果上面查出的信息不为空
			for (TblZjpsfp tzpf : zjpsfpList) {
				this.zjpsfpDAO.delZjpsfp(tzpf);// 将查出的所有信息对应的数据库记录删除
			}
		}

		if (null != expertIdArray && expertIdArray.length > 0) {// 判断如果expertIdArray不为空，且长度大于0
			for (int i = 0; i < expertIdArray.length; i++) {// 将参数构造成TblZjpsfp实体类，并插入数据库
				TblZjpsfp zjpsfp = new TblZjpsfp();
				SysXmpsmc sysXmpsmc = this.baseDataDAO.findXmpsmc(xmpsmcId);
				zjpsfp.setSysXmpsmc(sysXmpsmc);
				TblZhuanjiarukuxinxibiao tblZhuanjiarukuxinxibiao = this.expertManageDAO
						.findExpertById(expertIdArray[i]);
				zjpsfp.setTblZhuanjiarukuxinxibiao(tblZhuanjiarukuxinxibiao);
				zjpsfp.setUsername(zjpsfpModel.getUsername());
				zjpsfp.setYear(zjpsfpModel.getYear());
				zjpsfp.setFprq(zjpsfpModel.getFprq());
				zjpsfp.setNote(zjpsfpModel.getNote());
				this.zjpsfpDAO.saveZjpsfp(zjpsfp);
			}
		}

	}

	/**
	 * 将SysZhuanyema转化为StaticModel
	 * 
	 * @param zhuanyema
	 * @return
	 */
	public StaticModel getStaticModelByZhuanyeMa(SysZhuanyema zhuanyema) {
		StaticModel sm = new StaticModel();
		sm.setId(zhuanyema.getBm());
		sm.setName(zhuanyema.getShM());
		return sm;
	}

	/**
	 * 将TblZhuanjiarukuxinxibiao转化为StaticModel
	 * 
	 * @param expert
	 * @return
	 */
	public StaticModel getStaticModelByExpert(TblZhuanjiarukuxinxibiao expert) {
		StaticModel sm = new StaticModel();
		sm.setId(expert.getFBh().toString());
		sm.setName(expert.getXm());
		return sm;
	}

	/**
	 * 将TblZhuanjiarukuxinxibiao转化为BaseModel
	 * 
	 * @param expert
	 * @return
	 */
	public BaseModel getBaseModelByExpert(TblZhuanjiarukuxinxibiao expert) {
		BaseModel sm = new BaseModel();
		sm.setId(expert.getFBh().toString());
		sm.setName(expert.getXm());
		return sm;
	}

	/**
	 * 判断一个整数data是否在整数列表datas中 如果在则返回true，否则返回false
	 * 
	 * @param data
	 * @param datas
	 * @return
	 */
	public boolean isExitInArray(int data, List<Integer> datas) {
		boolean bl = false;
		for (Integer i : datas) {
			if (data == i) {
				bl = true;
				break;
			}
		}
		return bl;
	}
}
